import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { mars3dPlugin } from "vite-plugin-mars3d"
import path from "path"
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import wasm from '@rollup/plugin-wasm';
import copy from 'rollup-plugin-copy';
import viteCompression from 'vite-plugin-compression'

function resolve(dir) {
  return path.join(__dirname, dir)
}
const Timestamp = new Date().getTime();
export default ({ mode }) => {
  const envResolve = (mode: string, env: string) => loadEnv(mode, process.cwd())[env];
	return defineConfig({
    base: './',
    optimizeDeps: {
      include: [], // 确保 include 是一个空数组
      exclude: ['onnxruntime-web'],
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, "./src"),
        "@assets": path.resolve(__dirname, "./src/assets"),
        "@common": path.resolve(__dirname, "./src/common"),
        "@utils": path.resolve(__dirname, "./src/utils"),
        "@components": path.resolve(__dirname, "./src/components"),
        "@views": path.resolve(__dirname, "./src/views"),
        "@styles": path.resolve(__dirname, "./src/styles"),
      },
    },
    // 服务配置
    server:{
      host: '0.0.0.0',
      port:9998,// 端口号
      open:true,// 自动在浏览器打开
      https: false,// 是否开启 https
      fs: {
        strict: false,
      },
      proxy: {
        '/dev-api': {
            // target: `http://192.168.110.111:8100`,
            target: `http://192.168.110.100:8107`,
            // target: `http://192.168.110.111:8107`,
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/dev-api/, '')
        },
        // [envResolve.VITE_APP_BASE_API]: {
        //     // target: `http://192.168.110.111:8100`,
        //     target: `http://192.168.110.100:8080`,
        //     // target: `http://192.168.110.113:8107`,
        //     changeOrigin: true,
        //     rewrite: (path) => path.replace(/^\/dev-api/, '')
        // },
        '/obs': {
          // target: `http://192.168.110.100:9000`,
          target: `http://www.ldsk.obs.zxmap.cn`,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/obs/, '')
        },
        '/wms-layer': {
          target: `http://www.zxmap.cn:8088`,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/wms-layer/, '')
        },
        '/zxchhwy': {
          target: `https://zxch.cpolar.cn/`, //请求后台接口
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/zxchhwy/, ""),
          pathRewrite: {
              '^/zxchhwy': '' // 重写请求
          }
        },
        '/zxchjfj': {
          target: `http://139.9.173.189:8090/`, //请求后台接口
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/zxchjfj/, ""),
          pathRewrite: {
              '^/zxchjfj': '' // 重写请求
          }
        },
        '/zxchjfn': {
          target: `http://139.9.173.189:8091/`, //请求后台接口
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/zxchjfn/, ""),
          pathRewrite: {
              '^/zxchjfn': '' // 重写请求
          }
        },
        '/zxchjf': {
          target: `http://139.9.173.189:8080/`, //请求后台接口
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/zxchjf/, ""),
          pathRewrite: {
              '^/zxchjf': '' // 重写请求
          }
        },
        '/ceshi': {
          target: `http://139.9.173.189:81/`, //请求后台接口
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/ceshi/, ""),
          pathRewrite: {
            '^/ceshi': '' // 重写请求
          }
        },
        '/gucheng': {
          target: `http://192.168.110.100:81/`, //请求后台接口
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/gucheng/, ""),
          pathRewrite: {
            '^/gucheng': '' // 重写请求
          }
        },
        // '/cscscs': {
        //   target: `https://data.mars3d.cn/`, //请求后台接口
        //   changeOrigin: true, // 允许跨域
        //   rewrite: (path) => path.replace(/^\/cscscs/, ""),
        // },
      }
    },
    plugins: [
      vue(),
      mars3dPlugin(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      copy({
        targets: [
          { src: 'node_modules/onnxruntime-web/dist/*.wasm', dest: 'public/' },
        ],
      }),
      viteCompression({
        algorithm: 'brotliCompress', // 或者 'gzip'
        ext: '.br' // 或者 '.gz'
      })
    ],
    //  构建选项
    build: {
      //  输出目录，默认dist(相对于根目录)
      outDir: 'dist',
      //  指定生成静态资源的存放路径  ,,默认 assets
      assetsDir: 'assets',
      //小于此阈值的导入或引用资源将内联为 base64 编码，以避免额外的 http 请求。设置为 0 可以完全禁用此项。 默认4096 (4kb)
      assetsInlineLimit: 4096,
      //设置最终构建的浏览器兼容目标。默认值是一个 Vite 特有的值——'modules'，这是指 支持原生 ES 模块、原生 ESM 动态导入 和 import.meta 的浏览器。
      //  另一个特殊值是 “esnext” —— 即假设有原生动态导入支持，并且将会转译得尽可能小,一般会搭配esbuild使用
      target: 'modules',
      assetsInclude: /\.(wasm)$/,
      rollupOptions: {
        output: {
          chunkFileNames: `static/js/[name]-[hash]${Timestamp}.js`,
          entryFileNames: `static/js/[name]-[hash]${Timestamp}.js`,
          assetFileNames: `static/[ext]/[name]-[hash]${Timestamp}.[ext]`,
        },
        plugins: [wasm()],
        external: ['mars3d-cesium'], // 确保 cesium 被排除在打包之外
      }
    },
  })
}
